package com.example.ex3;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapTask implements Runnable{
    private Handler handler;
    private static final String TAG = "MapTask";

    public void setHandler(Handler handler){
        this.handler = handler;
    }

    @Override
    public void run() {
        Log.i(TAG,"run:run.......");

        try {
            Thread.sleep(3000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }

        Log.i(TAG,"run:run2");

        List<HashMap<String,String>> retList = new ArrayList<HashMap<String,String>>();
        try {
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
            Log.i(TAG,"run:title=");
            Elements tables = doc.getElementsByTag("table");
            Element table = tables.get(1);
            Elements trs = table.getElementsByTag("tr");
            for(Element tr : trs){
                Elements tds = tr.getElementsByTag("td");
                if(tds.size()>0){
                    String td1 = tds.get(0).text();
                    Log.i(TAG,"run:title=1");
                    String td2 = tds.get(5).text();
                    Log.i(TAG,"run:title=2");
                    Log.i(TAG,"run :"+td1+"-->"+td2);

                    HashMap<String,String> map=  new HashMap<String,String>();
                    map.put("cname",td1);
                    map.put("cval",td2);
                    retList.add(map);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        Message msg = handler.obtainMessage(7,retList);
        handler.sendMessage(msg);

    }
}
